package com.ct.code.tools;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URL;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ct.code.tools.domain.BaseInfo;
import com.ct.code.tools.domain.PropertyInfo;
import com.ct.code.tools.util.DateHelper;
import com.ct.code.tools.util.PinyinUtil;
import com.ct.code.tools.util.StringUtil;
import com.ct.code.tools.util.TypeMap;
import com.mysql.jdbc.StringUtils;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 * Hello world!
 *
 */
public class CodeToolsMain {
	private static Logger logger = LoggerFactory.getLogger(CodeToolsMain.class);

	public static void main(String[] args) throws Exception {
		Properties props = new Properties();
		props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("genTables.properties"));
		String basePackage = props.getProperty("basePackage");
		String tablePrefix = props.getProperty("tablePrefix");
		String moduleName = props.getProperty("moduleName");
		String tableNames = props.getProperty("tableNames");
		String dbtype = props.getProperty("db.type");
		String dburl = props.getProperty("db.url");
		String dbuser = props.getProperty("db.user");
		String dbpwd = props.getProperty("db.pwd");

		logger.info("basePackage={},tablePrefix={},moduleName={},tableNames={}", basePackage, tablePrefix, moduleName,
				tableNames);

		String[] tns = tableNames.split(",");
		List<BaseInfo> listBaseInfo = new ArrayList<BaseInfo>();
		for (String tableName : tns) {
			BaseInfo baseInfo = tableInfos(dbtype,dburl,dbuser,dbpwd,basePackage, moduleName, tableName);
			listBaseInfo.add(baseInfo);
		}

		URL url = Thread.currentThread().getContextClassLoader().getResource("genTables.properties");
		File file = new File(url.toURI());
		String ftlDir = file.getParentFile().getParentFile().getParentFile().getAbsolutePath() + File.separator + "ftl";
		System.out.println(ftlDir);

		generateMapper(listBaseInfo, ftlDir);
		
		generateDao(listBaseInfo,ftlDir);

		generateDomain(listBaseInfo,ftlDir);

		generateService(listBaseInfo,ftlDir);
		
		generateController(listBaseInfo,ftlDir);
		
		generateJspList(listBaseInfo,ftlDir);
		
		generateJspView(listBaseInfo,ftlDir);
		
		generateJspEdit(listBaseInfo,ftlDir);
		
		System.out.println("generate code over!!!!");
		System.exit(0);

	}
	
	private static void generateJspEdit(List<BaseInfo> listBaseInfo, String ftlDir) throws Exception {
		for(BaseInfo baseInfo : listBaseInfo){
			Configuration conf = new Configuration();
			conf.setDefaultEncoding("UTF-8");
			conf.setEncoding(Locale.CHINESE, "UTF-8");
			conf.setDirectoryForTemplateLoading(new File(ftlDir));
			conf.setObjectWrapper(new DefaultObjectWrapper());
			Template template = conf.getTemplate("jsp_edit.ftl", "UTF-8");
			
//			String generateCodeDir = new File(ftlLoadingDir).getParentFile().getAbsolutePath() 
//					+ File.separator + "generateCodeDir" + File.separator + baseInfo.getBasePackage().replaceAll("\\.", File.separator) + File.separator + "";
			
			String replaceFileSeparator = File.separator;
			if("\\".equals(replaceFileSeparator)){
				replaceFileSeparator = "\\\\";
			}
			
			String generateCodeDir = new File(ftlDir).getParentFile().getAbsolutePath() 
					+ File.separator + "generateCodeDir" + File.separator + "jsp" + File.separator + baseInfo.getModuleName();
			
			System.out.println(generateCodeDir);
			File file = new File(generateCodeDir
					+ File.separator + baseInfo.getObjectName() + "_edit.jsp");
			
			
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			if (!file.exists()) {
				file.createNewFile();
			}

			Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
			// Writer out = new BufferedWriter(new FileWriter(file));
			Map<String, Object> mapData = new HashMap<String, Object>();
			mapData.put("baseInfo", baseInfo);
			mapData.put("wholeClassName", baseInfo.getBasePackage() + ".domain." + baseInfo.getModuleName() +"." + baseInfo.getClassName());
			template.process(mapData, out);
			out.flush();
		}
	}
	
	private static void generateJspView(List<BaseInfo> listBaseInfo, String ftlDir) throws Exception {
		for(BaseInfo baseInfo : listBaseInfo){
			Configuration conf = new Configuration();
			conf.setDefaultEncoding("UTF-8");
			conf.setEncoding(Locale.CHINESE, "UTF-8");
			conf.setDirectoryForTemplateLoading(new File(ftlDir));
			conf.setObjectWrapper(new DefaultObjectWrapper());
			Template template = conf.getTemplate("jsp_view.ftl", "UTF-8");
			
//			String generateCodeDir = new File(ftlLoadingDir).getParentFile().getAbsolutePath() 
//					+ File.separator + "generateCodeDir" + File.separator + baseInfo.getBasePackage().replaceAll("\\.", File.separator) + File.separator + "";
			
			String replaceFileSeparator = File.separator;
			if("\\".equals(replaceFileSeparator)){
				replaceFileSeparator = "\\\\";
			}
			
			String generateCodeDir = new File(ftlDir).getParentFile().getAbsolutePath() 
					+ File.separator + "generateCodeDir" + File.separator + "jsp" + File.separator + baseInfo.getModuleName();
			
			System.out.println(generateCodeDir);
			File file = new File(generateCodeDir
					+ File.separator + baseInfo.getObjectName() + "_view.jsp");
			
			
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			if (!file.exists()) {
				file.createNewFile();
			}

			Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
			// Writer out = new BufferedWriter(new FileWriter(file));
			Map<String, Object> mapData = new HashMap<String, Object>();
			mapData.put("baseInfo", baseInfo);
			mapData.put("wholeClassName", baseInfo.getBasePackage() + ".domain." + baseInfo.getModuleName() +"." + baseInfo.getClassName());
			template.process(mapData, out);
			out.flush();
		}
	}
	
	private static void generateJspList(List<BaseInfo> listBaseInfo, String ftlDir) throws Exception {
		for(BaseInfo baseInfo : listBaseInfo){
			Configuration conf = new Configuration();
			conf.setDefaultEncoding("UTF-8");
			conf.setEncoding(Locale.CHINESE, "UTF-8");
			conf.setDirectoryForTemplateLoading(new File(ftlDir));
			conf.setObjectWrapper(new DefaultObjectWrapper());
			Template template = conf.getTemplate("jsp_list.ftl", "UTF-8");
			
//			String generateCodeDir = new File(ftlLoadingDir).getParentFile().getAbsolutePath() 
//					+ File.separator + "generateCodeDir" + File.separator + baseInfo.getBasePackage().replaceAll("\\.", File.separator) + File.separator + "";
			
			String replaceFileSeparator = File.separator;
			if("\\".equals(replaceFileSeparator)){
				replaceFileSeparator = "\\\\";
			}
			
			String generateCodeDir = new File(ftlDir).getParentFile().getAbsolutePath() 
					+ File.separator + "generateCodeDir" + File.separator + "jsp" + File.separator + baseInfo.getModuleName();
			
			System.out.println(generateCodeDir);
			File file = new File(generateCodeDir
					+ File.separator + baseInfo.getObjectName() + "_list.jsp");
			
			
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			if (!file.exists()) {
				file.createNewFile();
			}

			Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
			// Writer out = new BufferedWriter(new FileWriter(file));
			Map<String, Object> mapData = new HashMap<String, Object>();
			mapData.put("baseInfo", baseInfo);
			mapData.put("wholeClassName", baseInfo.getBasePackage() + ".domain." + baseInfo.getModuleName() +"." + baseInfo.getClassName());
			template.process(mapData, out);
			out.flush();
		}
	}
	

	private static void generateController(List<BaseInfo> listBaseInfo, String ftlDir) throws Exception {
		for(BaseInfo baseInfo : listBaseInfo){
			Configuration conf = new Configuration();
			conf.setDefaultEncoding("UTF-8");
			conf.setEncoding(Locale.CHINESE, "UTF-8");
			conf.setDirectoryForTemplateLoading(new File(ftlDir));
			conf.setObjectWrapper(new DefaultObjectWrapper());
			Template template = conf.getTemplate("Controller.ftl", "UTF-8");
			
//			String generateCodeDir = new File(ftlLoadingDir).getParentFile().getAbsolutePath() 
//					+ File.separator + "generateCodeDir" + File.separator + baseInfo.getBasePackage().replaceAll("\\.", File.separator) + File.separator + "";
			
			String replaceFileSeparator = File.separator;
			if("\\".equals(replaceFileSeparator)){
				replaceFileSeparator = "\\\\";
			}
			
			String generateCodeDir = new File(ftlDir).getParentFile().getAbsolutePath() 
					+ File.separator + "generateCodeDir" + File.separator
					+ baseInfo.getBasePackage().replaceAll("\\.", replaceFileSeparator)
					+ File.separator + "controller" 
					+ File.separator + baseInfo.getModuleName();
			
			System.out.println(generateCodeDir);
			File file = new File(generateCodeDir
					+ File.separator + baseInfo.getClassName() + "Controller.java");
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			if (!file.exists()) {
				file.createNewFile();
			}

			Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
			// Writer out = new BufferedWriter(new FileWriter(file));
			Map<String, Object> mapData = new HashMap<String, Object>();
			mapData.put("baseInfo", baseInfo);
			mapData.put("wholeClassName", baseInfo.getBasePackage() + ".domain." + baseInfo.getModuleName() +"." + baseInfo.getClassName());
			template.process(mapData, out);
			out.flush();
		}
	}

	private static void generateDao(List<BaseInfo> listBaseInfo, String ftlDir) throws IOException, TemplateException {
		for(BaseInfo baseInfo : listBaseInfo){
			Configuration conf = new Configuration();
			conf.setDefaultEncoding("UTF-8");
			conf.setEncoding(Locale.CHINESE, "UTF-8");
			conf.setDirectoryForTemplateLoading(new File(ftlDir));
			conf.setObjectWrapper(new DefaultObjectWrapper());
			Template template = conf.getTemplate("Dao.ftl", "UTF-8");
			
//			String generateCodeDir = new File(ftlLoadingDir).getParentFile().getAbsolutePath() 
//					+ File.separator + "generateCodeDir" + File.separator + baseInfo.getBasePackage().replaceAll("\\.", File.separator) + File.separator + "";
			
			String replaceFileSeparator = File.separator;
			if("\\".equals(replaceFileSeparator)){
				replaceFileSeparator = "\\\\";
			}
			
			String generateCodeDir = new File(ftlDir).getParentFile().getAbsolutePath() 
					+ File.separator + "generateCodeDir" + File.separator
					+ baseInfo.getBasePackage().replaceAll("\\.", replaceFileSeparator)
					+ File.separator + "dao" 
					+ File.separator + baseInfo.getModuleName();
			
			System.out.println(generateCodeDir);
			File file = new File(generateCodeDir
					+ File.separator + baseInfo.getClassName() + "Dao.java");
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			if (!file.exists()) {
				file.createNewFile();
			}

			Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
			// Writer out = new BufferedWriter(new FileWriter(file));
			Map<String, Object> mapData = new HashMap<String, Object>();
			mapData.put("baseInfo", baseInfo);
			mapData.put("wholeClassName", baseInfo.getBasePackage() + ".domain." + baseInfo.getModuleName() +"." + baseInfo.getClassName());
			template.process(mapData, out);
			out.flush();
		}
	}

	private static void generateService(List<BaseInfo> listBaseInfo,String ftlDir) throws Exception {
		for(BaseInfo baseInfo : listBaseInfo){
			Configuration conf = new Configuration();
			conf.setDefaultEncoding("UTF-8");
			conf.setEncoding(Locale.CHINESE, "UTF-8");
			conf.setDirectoryForTemplateLoading(new File(ftlDir));
			conf.setObjectWrapper(new DefaultObjectWrapper());
			Template template = conf.getTemplate("Service.ftl", "UTF-8");
			
//			String generateCodeDir = new File(ftlLoadingDir).getParentFile().getAbsolutePath() 
//					+ File.separator + "generateCodeDir" + File.separator + baseInfo.getBasePackage().replaceAll("\\.", File.separator) + File.separator + "";
			
			String replaceFileSeparator = File.separator;
			if("\\".equals(replaceFileSeparator)){
				replaceFileSeparator = "\\\\";
			}
			
			String generateCodeDir = new File(ftlDir).getParentFile().getAbsolutePath() 
					+ File.separator + "generateCodeDir" + File.separator
					+ baseInfo.getBasePackage().replaceAll("\\.", replaceFileSeparator)
					+ File.separator + "service" 
					+ File.separator + baseInfo.getModuleName();
			
			System.out.println(generateCodeDir);
			File file = new File(generateCodeDir
					+ File.separator + baseInfo.getClassName() + "Service.java");
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			if (!file.exists()) {
				file.createNewFile();
			}

			Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
			// Writer out = new BufferedWriter(new FileWriter(file));
			Map<String, Object> mapData = new HashMap<String, Object>();
			mapData.put("baseInfo", baseInfo);
			mapData.put("wholeClassName", baseInfo.getBasePackage() + ".domain." + baseInfo.getModuleName() +"." + baseInfo.getClassName());
			template.process(mapData, out);
			out.flush();
		}
	}

	private static void generateDomain(List<BaseInfo> listBaseInfo,String ftlDir) throws Exception {
		for(BaseInfo baseInfo : listBaseInfo){
			Configuration conf = new Configuration();
			conf.setDefaultEncoding("UTF-8");
			conf.setEncoding(Locale.CHINESE, "UTF-8");
			conf.setDirectoryForTemplateLoading(new File(ftlDir));
			conf.setObjectWrapper(new DefaultObjectWrapper());
			Template template = conf.getTemplate("Domain.ftl", "UTF-8");
			
//			String generateCodeDir = new File(ftlLoadingDir).getParentFile().getAbsolutePath() 
//					+ File.separator + "generateCodeDir" + File.separator + baseInfo.getBasePackage().replaceAll("\\.", File.separator) + File.separator + "";
			
			String replaceFileSeparator = File.separator;
			if("\\".equals(replaceFileSeparator)){
				replaceFileSeparator = "\\\\";
			}
			
			String generateCodeDir = new File(ftlDir).getParentFile().getAbsolutePath() 
					+ File.separator + "generateCodeDir" + File.separator
					+ baseInfo.getBasePackage().replaceAll("\\.", replaceFileSeparator)
					+ File.separator + "domain" 
					+ File.separator + baseInfo.getModuleName();
			
			System.out.println(generateCodeDir);
			File file = new File(generateCodeDir
					+ File.separator + baseInfo.getClassName() + ".java");
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			if (!file.exists()) {
				file.createNewFile();
			}

			Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
			// Writer out = new BufferedWriter(new FileWriter(file));
			Map<String, Object> mapData = new HashMap<String, Object>();
			mapData.put("baseInfo", baseInfo);
			mapData.put("wholeClassName", baseInfo.getBasePackage() + ".domain." + baseInfo.getModuleName() +"." + baseInfo.getClassName());
			template.process(mapData, out);
			out.flush();
		}
	}

	private static void generateMapper(List<BaseInfo> listBaseInfo, String ftlLoadingDir) throws Exception{
		for(BaseInfo baseInfo : listBaseInfo){
			Configuration conf = new Configuration();
			conf.setDefaultEncoding("UTF-8");
			conf.setEncoding(Locale.CHINESE, "UTF-8");
			conf.setDirectoryForTemplateLoading(new File(ftlLoadingDir));
			conf.setObjectWrapper(new DefaultObjectWrapper());
			Template template = conf.getTemplate("Mapper.ftl", "UTF-8");
			
//			String generateCodeDir = new File(ftlLoadingDir).getParentFile().getAbsolutePath() 
//					+ File.separator + "generateCodeDir" + File.separator + baseInfo.getBasePackage().replaceAll("\\.", File.separator) + File.separator + "";
			
			String generateCodeDir = new File(ftlLoadingDir).getParentFile().getAbsolutePath() 
					+ File.separator + "generateCodeDir" + File.separator + "mybatis" + File.separator + baseInfo.getModuleName();
			
			System.out.println(generateCodeDir);
			File file = new File(generateCodeDir
					+ File.separator + baseInfo.getClassName() + "Mapper.xml");
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			if (!file.exists()) {
				file.createNewFile();
			}

			Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
			// Writer out = new BufferedWriter(new FileWriter(file));
			Map<String, Object> mapData = new HashMap<String, Object>();
			mapData.put("baseInfo", baseInfo);
			mapData.put("wholeClassName", baseInfo.getBasePackage() + ".domain." + baseInfo.getModuleName() +"." + baseInfo.getClassName());
			template.process(mapData, out);
			out.flush();
		}
	}

	public static String parse(String all) {  
        String comment = null;  
        int index = all.indexOf("COMMENT='");  
        if (index < 0) {  
            return "";  
        }  
        comment = all.substring(index + 9);  
        comment = comment.substring(0, comment.length() - 1);  
        return comment;  
    } 
	
	public static BaseInfo tableInfos(final String dbType,final String url,final String user,final String pwd,String basePackage, String moduleName, String tableName) throws Exception {
//		final String dbType = "mysql";
//		final String url = "jdbc:mysql://114.215.236.14:3306/ctdb?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8";
//		final String user = "root";
//		final String pwd = "lianpai!@#";
		
//		final String dbType = "mysql";
//		final String url = "jdbc:mysql://59.110.172.176:3306/huiceshi?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8";
//		final String user = "shop001";
//		final String pwd = "Hnzhang#@";
		
//		final String dbType = "mysql";
//		final String url = "jdbc:mysql://localhost:3306/admintest?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8";
//		final String user = "root";
//		final String pwd = "zzqiang";

		String className = StringUtil.tableName2JavaName(tableName, true);

		Connection cn = null;
		tableName = tableName.toUpperCase();
		String developer = "zhiqiang.zhang";
		String date = DateHelper.nowString(DateHelper.DATE_PATTERN_DEFAULT);
		String namespace = basePackage + ".domain." + moduleName + "." + className + "Mapper";

		try {
			tableName = tableName.toUpperCase();
			Class.forName("com.mysql.jdbc.Driver");
			Properties props = new Properties();
			props.put("remarksReporting", "true");
			props.put("password", pwd);
			props.put("user", user);
			cn = DriverManager.getConnection(url, props);

			BaseInfo baseInfo = new BaseInfo();
			
			 Statement stmt = cn.createStatement();  
            ResultSet rset = stmt.executeQuery("SHOW CREATE TABLE " + tableName);  
            if (rset != null && rset.next()) {  
                String createDDL = rset.getString(2);  
                String comment = parse(createDDL);  
                System.out.println(createDDL);
               System.out.println(comment);
               
               baseInfo.setTableNameRemark(comment);
            }  
			
			Statement st = cn.createStatement();
			ResultSet rst = st.executeQuery("select * from " + tableName + " where 1=1 limit 0,1");
			System.out.println(rst.getMetaData().getColumnCount());
			int realColumnCount = rst.getMetaData().getColumnCount();

			DatabaseMetaData m_DBMetaData = cn.getMetaData();
			// 4. 提取表内的字段的名字和类型
			String columnName;
			String columnType;
			ResultSet colRet = m_DBMetaData.getColumns(null, "%", tableName, "%");

			int queryColumnCount = colRet.getMetaData().getColumnCount();
			System.out.println(colRet.getMetaData().getColumnCount());

			
			baseInfo.setClassName(className);
			
			String objectName = StringUtil.firstLower(className);
			baseInfo.setObjectName(objectName);
			
			baseInfo.setModuleName(moduleName);
			baseInfo.setBasePackage(basePackage);
			baseInfo.setTableName(tableName);
			baseInfo.setUser(developer);
			baseInfo.setDate(date);
			baseInfo.setMybatisNamespace(namespace);

			PropertyInfo propertyInfo = null;
			List<PropertyInfo> listPropertyInfo = new ArrayList<PropertyInfo>();
			List<PropertyInfo> listSearchPropertyInfo = new ArrayList<PropertyInfo>();
			List<PropertyInfo> listSelectPropertyInfo = new ArrayList<PropertyInfo>();
			String remarks;
			int setColCount = 0;
			while (colRet.next()  && setColCount < realColumnCount) {
				propertyInfo = new PropertyInfo();
				columnName = colRet.getString("COLUMN_NAME");
				columnType = colRet.getString("TYPE_NAME");
				System.out.println(columnName + "::" + columnType);
				remarks = colRet.getString("REMARKS");
				if(isJson(remarks)){
					Map map = JSON.parseObject(remarks, Map.class);
					
					
					String type = (String)map.get("type");
					
					if("inputSearch".equalsIgnoreCase(type)){
						//作为搜索条件
						remarks = (String)map.get("remarks");
						propertyInfo.setDestForType("inputSearch");
						String searchType = (String)map.get("searchType");
						propertyInfo.setSearchType(searchType);
					}else if("dict".equalsIgnoreCase(type)){
						propertyInfo.setSearchType("eq");
						
						String dictCode = (String)map.get("dictCode");
						String dictName = (String)map.get("dictName");
						propertyInfo.setDictParentCode(dictCode);
						propertyInfo.setDestForType("select");
						remarks = dictName;
						System.out.println("============= dict for programe please copy===========");
						
						System.out.println("insert into td_sm_dict (dict_code,dict_name,sort_val,parent_code,parent_id,remark) values('"+dictCode+"','"+dictName+"',1,0,0,'"+dictName+"');");
						
						List<Map> listDict  = (List<Map>) map.get("listDict");
						for(Map subDictMap : listDict){
							String subDictCode = (String)subDictMap.get("dictCode");
							String subDictName = (String)subDictMap.get("dictName");
							System.out.println("insert into td_sm_dict (dict_code,dict_name,sort_val,parent_code,parent_id,remark) values('"+subDictCode+"','"+subDictName+"',1,'"+dictCode+"',0,'"+subDictName+"');");
						}
						System.out.println("----- parent_id需要修改!");
						
						for(Map subDictMap : listDict){
							String subDictCode = (String)subDictMap.get("dictCode");
							String subDictName = (String)subDictMap.get("dictName");
							System.out.println("/**");
							System.out.println("*" + dictName + " " + subDictCode + ":" + subDictName);
							System.out.println("*/");
							System.out.println("public static final String " + dictCode.toLowerCase() + "_" + PinyinUtil.getPingYin(subDictName).toLowerCase() + " = \"" + subDictCode + "\";");
							
						}
						
					}
					//public static final String res_type_smmenu = "smMenu";
					//insert into td_sm_dict (dict_code,dict_name,sort_val,parent_code,parent_id,remark) values('','',1,0,0,'');
				}
				propertyInfo.setRemarks(remarks);
				
				propertyInfo.setColumnName(columnName.toUpperCase());
				propertyInfo.setColumnType(columnType);
				propertyInfo.setPropertyName(StringUtil.fieldName2JavaName(columnName, false));
				String javaType = TypeMap.getJavaType(dbType, columnType);
				propertyInfo.setPropertyType(javaType);
				propertyInfo.setJdbcType(TypeMap.javaType2jdbcType(javaType));
				if ("DATE".equals(columnType)) {
					baseInfo.setExistDateType(true);
				}
				listPropertyInfo.add(propertyInfo);
				if(propertyInfo.getDestForType() != null && !"".equals(propertyInfo.getDestForType())){
					if("inputSearch".equalsIgnoreCase(propertyInfo.getDestForType())){
						listSearchPropertyInfo.add(propertyInfo);
					}else if("select".equalsIgnoreCase(propertyInfo.getDestForType())){
						listSelectPropertyInfo.add(propertyInfo);
					}
				}
				if (realColumnCount != queryColumnCount) {
					setColCount++;
				}
			}
			// 获取主键
			ResultSet rs = m_DBMetaData.getPrimaryKeys(cn.getCatalog(), null, tableName);
			String primaryKeyColumnName = "";
			while (rs.next()) {
				primaryKeyColumnName = rs.getString("COLUMN_NAME");
				System.out.println(primaryKeyColumnName);
			}

			baseInfo.setPrimaryKey(primaryKeyColumnName);
			baseInfo.setPrimaryKeyProperty(StringUtil.fieldName2JavaName(primaryKeyColumnName, false));
			baseInfo.setPropertyInfos(listPropertyInfo);
			baseInfo.setListSearchPropertyInfo(listSearchPropertyInfo);
			baseInfo.setListSelectPropertyInfo(listSelectPropertyInfo);
			System.out.println(baseInfo);

			return baseInfo;

		} finally {
			if (null != cn)
				cn.close();
		}
	}
	
	public static boolean isJson(String content){
	    try {
	        JSONObject jsonStr= JSONObject.parseObject(content);
	        return  true;
	   } catch (Exception e) {
	        return false;
	  }
	}
}
